package com.rtx.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.rtx.edi.gatway.cache.EdiApiDataContext;
import com.rtx.edi.gatway.cache.EdiColumnValue;
import com.rtx.edi.platform.rule.bean.UpdRule;

public class EdiSqlBuilder {
	
	public static List<String> createUpdSql(EdiApiDataContext context) {
		
		List<String> sqlList = new ArrayList<String>();
		String mainSql = createUpdSqlByColumnValue(context.getMainTable());
		sqlList.add(mainSql);
		
		if(context.getSubtableMap() == null) {
			return sqlList;
		}
		
		for(String subTableName:context.getSubtableMap().keySet()) {
			Map<Integer,List<EdiColumnValue>> subTableRows = context.getSubtableMap().get(subTableName);
			for(Integer index:subTableRows.keySet()) {
				List<EdiColumnValue> subTableRow =  subTableRows.get(index);
				//是否跳过生成更新语句
				if("true".equals(subTableRow.get(0).getSkipupdate())) {
					continue;
				}
				String subRowInsertSql = createUpdSqlByColumnValue(subTableRows.get(index));
				sqlList.add(subRowInsertSql);
			}
		}
		
		return sqlList;
	}
	
	//处理更新规则
	void handleUpdRule(List<EdiColumnValue> columnValues){
		EdiColumnValue cv = columnValues.get(0);
		UpdRule updrule = cv.getUpdRule();
		if(updrule != null) {
			
		}
	}
	
	public static String createUpdSqlByColumnValue(List<EdiColumnValue> columnValues) {
		EdiColumnValue firstEdiColumnValue = columnValues.get(0);
		String tablename = firstEdiColumnValue.getTablename();
		List<String> condition= new ArrayList<String>();
		List<String> updField = new ArrayList<String>();
		
		for(EdiColumnValue temp:columnValues) {
			//标记为 需要被更新字段
			if("true".equals(temp.getUpdColumn())) {
				String columnname = temp.getColumnname();
				String value = temp.getValue()+"";
				updField.add(columnname+"='"+value+"'");
			}else if("true".equals(temp.getUpdCondition())) {
				String columnname = temp.getColumnname();
				String value = temp.getValue()+"";
				condition.add(" and "+columnname+"='"+value+"'");
			}
		}
		
		StringBuffer updSql = new StringBuffer();
		updSql.append("update ");
		updSql.append(" "+tablename+" ");
		updSql.append(" set ");
		updSql.append(" "+updField.stream().collect(Collectors.joining(","))+" ");
		updSql.append(" where 1=1");
		updSql.append(condition.stream().collect(Collectors.joining("")));
		
		if(condition.size() < 1) {
			throw new RuntimeException("生成更新语句-更新条件为空-将更新全表数据-请检查配置!"+updSql.toString());
		}
		if(updField.size() < 1) {
			throw new RuntimeException("生成更新语句-更新字段为空请检查配置"+updSql.toString());
		}
		return updSql.toString();
	}

	public static List<String> createInsertSql(EdiApiDataContext context) {
		
		List<String> sqlList = new ArrayList<String>();
		String mainSql = createSqlByColumnValue(context.getMainTable());
		sqlList.add(mainSql);
		
		if(context.getSubtableMap() == null) {
			return sqlList;
		}
		
		for(String subTableName:context.getSubtableMap().keySet()) {
			Map<Integer,List<EdiColumnValue>> subTableRows = context.getSubtableMap().get(subTableName);
			for(Integer index:subTableRows.keySet()) {
				String subRowInsertSql = createSqlByColumnValue(subTableRows.get(index));
				sqlList.add(subRowInsertSql);
			}
		}
		
		return sqlList;
	}
	
	public static String createSqlByColumnValue(List<EdiColumnValue> columnValues) {
		EdiColumnValue firstEdiColumnValue = columnValues.get(0);
		String tablename = firstEdiColumnValue.getTablename();
		List<String> columns = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		
		for(EdiColumnValue temp:columnValues) {
			if("true".equals(temp.getSkipinsert())) {
				continue;
			}
			columns.add(temp.getColumnname());
			values.add("'"+temp.getValue()+"'");
		}
		
		StringBuffer insertSql = new StringBuffer();
		insertSql.append("insert into");
		insertSql.append(" "+tablename+" ");
		insertSql.append("(");
		insertSql.append(columns.stream().collect(Collectors.joining(",")));
		insertSql.append(")");
		insertSql.append("values");
		insertSql.append("(");
		insertSql.append(values.stream().collect(Collectors.joining(",")));
		insertSql.append(")");
		
		return insertSql.toString();
	}
}
